"""优化
使用差分数组
"""

def main():
    import sys
    input = sys.stdin

    N = int(input.readline())
    couples = {}
    # has表存储 情侣个数
    for _ in range(N):
        a, b = input.readline().split()
        couples[a] = b
        couples[b] = a

    M = int(input.readline())
    # 数组
    a = input.readline().split()

    # 预处理每个ID的位置, pos存储字典{人：下标}
    pos = {people: idx for idx, people in enumerate(a)}
    # 将客人ID列表转换为集合，方便后续快速判断某个ID是否在场, 减少时间复杂度
    all_people = set(a)

    # 标记单身人士的位置
    is_single = [False] * M
    for idx, person in enumerate(a):
        if person not in couples or couples[person] not in all_people:
            is_single[idx] = True

    # 初始化差分数组，长度为M+1，用于记录非相邻情侣对中间区域的影响情况
    diff = [0] * (M + 1)
    # 初始化影响数组，长度为M，用于记录每个客人受到情侣撒狗粮的影响次数
    impact = [0] * M

    # 用于存储已经处理过的情侣ID的集合
    processed = set()
    for i in range(M):
        cur = a[i]
        # 如果当前客人在情侣字典中且还未被处理过
        if cur in couples and cur not in processed:
            partner = couples[cur]  # 情侣
            if partner in all_people:
                processed.add(cur)
                processed.add(partner)
                j = pos[partner]
                if abs(i - j) == 1:
                    # 相邻情况：直接处理左右位置
                    left = i - 1
                    if left >= 0 and is_single[left]:
                        impact[left] += 1
                    right = j + 1
                    if right < M and is_single[right]:
                        impact[right] += 1
                else:
                    # 非相邻情况：使用差分数组记录中间区间
                    start = min(i, j) + 1
                    end = max(i, j)
                    if start < end:
                        diff[start] += 1
                        diff[end] -= 1

    # 计算差分数组的前缀和，得到中间区域的影响次数
    cur_diff = 0
    for k in range(M):
        cur_diff += diff[k]
        impact[k] += cur_diff

    # 统计单身人士的总次数
    count = {}
    for idx in range(M):
        if is_single[idx]:
            # person = a[idx]
            # 获取当前单身客人的影响次数
            count[a[idx]] = impact[idx]

    if not count:
        # 如果没有单身客人，即count字典为空，则输出空行
        print("")
        return

    max_count = max(count.values())
    victims = [k for k, v in count.items() if v == max_count]
    victims.sort()
    print(' '.join(victims))

if __name__ == "__main__":
    main()